using System;
using UnityEngine;

namespace ProceduralPrimitivesUtil
{
	public class Tube : PPBase
	{
		[Header("Basic parameters")]
		public float radius1 = 0.5f;

		public float radius2 = 0.3f;

		public float height = 1f;

		public bool cap1;

		public float capThickness1 = 0.2f;

		public bool cap2;

		public float capThickness2 = 0.2f;

		[Header("Segments")]
		public int sides = 20;

		public int capSegs = 2;

		public int heightSegs = 2;

		[Header("Slice")]
		public bool sliceOn;

		public float sliceFrom;

		public float sliceTo = 360f;

		[Header("Mapping Coordinates")]
		public bool generateMappingCoords = true;

		public bool realWorldMapSize;

		public Vector2 UVOffset = new Vector2(0f, 0f);

		public Vector2 UVTiling = new Vector2(1f, 1f);

		[Header("Others")]
		public bool flipNormals;

		public bool smooth = true;

		protected override void CreateMesh()
		{
			m_mesh.name = "Tube";
			radius1 = Mathf.Clamp(radius1, 1E-05f, 10000f);
			radius2 = Mathf.Clamp(radius2, 1E-05f, radius1);
			height = Mathf.Clamp(height, 1E-05f, 10000f);
			if (cap1)
			{
				capThickness1 = Mathf.Clamp(capThickness1, 1E-05f, height);
				capThickness2 = Mathf.Clamp(capThickness2, 1E-05f, height - capThickness1);
			}
			else
			{
				capThickness1 = Mathf.Clamp(capThickness1, 1E-05f, height);
				capThickness2 = Mathf.Clamp(capThickness2, 1E-05f, height);
			}
			sides = Mathf.Clamp(sides, 3, 100);
			capSegs = Mathf.Clamp(capSegs, 1, 100);
			heightSegs = Mathf.Clamp(heightSegs, 1, 100);
			sliceFrom = Mathf.Clamp(sliceFrom, 0f, 360f);
			sliceTo = Mathf.Clamp(sliceTo, sliceFrom, 360f);
			float num = height * 0.5f;
			float num2 = (cap1 ? capThickness1 : 0f);
			float num3 = (cap2 ? capThickness2 : 0f);
			float num4 = radius1 - radius2;
			Vector3 vector = new Vector3(0f, 0f - num, 0f);
			Vector3 vector2 = new Vector3(0f, num, 0f);
			Vector3 vector3 = new Vector3(0f, 0f - num + num2, 0f);
			Vector3 vector4 = new Vector3(0f, num - num3, 0f);
			CreateCylinder(pivotOffset + Vector3.zero, m_rotation * Vector3.forward, m_rotation * Vector3.right, height, radius1, sides, heightSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals, smooth);
			CreateCylinder(pivotOffset + m_rotation * new Vector3(0f, (num2 - num3) * 0.5f, 0f), m_rotation * Vector3.forward, m_rotation * Vector3.right, height - num3 - num2, radius2, sides, heightSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals, smooth);
			if (cap1)
			{
				CreateCircle(pivotOffset + m_rotation * vector, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius1, sides, capSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals);
				CreateCircle(pivotOffset + m_rotation * vector3, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius2, sides, capSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals);
			}
			else
			{
				CreateRing(pivotOffset + m_rotation * vector, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius1, radius2, sides, capSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals);
			}
			if (cap2)
			{
				CreateCircle(pivotOffset + m_rotation * vector2, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius1, sides, capSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals);
				CreateCircle(pivotOffset + m_rotation * vector4, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius2, sides, capSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, !flipNormals);
			}
			else
			{
				CreateRing(pivotOffset + m_rotation * vector2, m_rotation * Vector3.forward, m_rotation * Vector3.right, radius1, radius2, sides, capSegs, sliceOn, sliceFrom, sliceTo, generateMappingCoords, realWorldMapSize, UVOffset, UVTiling, flipNormals);
			}
			if (sliceOn)
			{
				Vector3 vector5 = new Vector3(Mathf.Sin(sliceFrom * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceFrom * (MathF.PI / 180f)));
				Vector3 vector6 = new Vector3(Mathf.Sin(sliceTo * (MathF.PI / 180f)), 0f, Mathf.Cos(sliceTo * (MathF.PI / 180f)));
				Vector2 vector7 = (realWorldMapSize ? new Vector2(1f, 1f) : new Vector2(0.5f, 1f));
				vector7 = new Vector2(UVTiling.x * vector7.x, UVTiling.y * vector7.y);
				CreatePlane(pivotOffset + m_rotation * vector5 * (radius2 + num4 * 0.5f), m_rotation * Vector3.up, m_rotation * -vector5.normalized, num4, height, capSegs, heightSegs, generateMappingCoords, realWorldMapSize, UVOffset, vector7, flipNormals);
				CreatePlane(pivotOffset + m_rotation * vector6 * (radius2 + num4 * 0.5f), m_rotation * Vector3.up, m_rotation * vector6.normalized, num4, height, capSegs, heightSegs, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(0.5f, 0f), vector7, flipNormals);
				if (cap1)
				{
					CreatePlane(pivotOffset + m_rotation * new Vector3(vector5.x * radius2 * 0.5f, 0f - num + capThickness1 * 0.5f, vector5.z * radius2 * 0.5f), m_rotation * Vector3.up, m_rotation * -vector5.normalized, radius2, capThickness1, capSegs, 2, generateMappingCoords, realWorldMapSize, UVOffset, vector7, flipNormals);
					CreatePlane(pivotOffset + m_rotation * new Vector3(vector6.x * radius2 * 0.5f, 0f - num + capThickness1 * 0.5f, vector6.z * radius2 * 0.5f), m_rotation * Vector3.up, m_rotation * vector6.normalized, radius2, capThickness1, capSegs, 2, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(0.5f, 0f), vector7, flipNormals);
				}
				if (cap2)
				{
					CreatePlane(pivotOffset + m_rotation * new Vector3(vector5.x * radius2 * 0.5f, num - capThickness2 * 0.5f, vector5.z * radius2 * 0.5f), m_rotation * Vector3.up, m_rotation * -vector5.normalized, radius2, capThickness2, capSegs, 2, generateMappingCoords, realWorldMapSize, UVOffset, vector7, flipNormals);
					CreatePlane(pivotOffset + m_rotation * new Vector3(vector6.x * radius2 * 0.5f, num - capThickness2 * 0.5f, vector6.z * radius2 * 0.5f), m_rotation * Vector3.up, m_rotation * vector6.normalized, radius2, capThickness2, capSegs, 2, generateMappingCoords, realWorldMapSize, UVOffset + new Vector2(0.5f, 0f), vector7, flipNormals);
				}
			}
		}
	}
}
